package com.cskaoyan.config;

import com.cskaoyan.annotation.LogRecord;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.po.MarketAdmin;
import com.cskaoyan.bean.po.MarketLog;
import com.cskaoyan.constant.Constant;
import com.cskaoyan.mapper.MarketLogMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;


/**
 * @author LiangRuiJiang
 * @since 2022/06/06  21:25
 */
@Component
@Aspect
public class AspectConfiguration {

    @Autowired
    MarketLogMapper marketLogMapper;

    // @Autowired
    // LogFilter logFilter;

    @Around("@annotation(com.cskaoyan.annotation.LogRecord)")
    // @Around("execution(* com.cskaoyan.controller.AdminController..*(..))")
    @Transactional
    public BaseRespVo around(ProceedingJoinPoint joinpoint) throws Throwable {
        //执行方法前
        MarketLog log = new MarketLog();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        //管理员Ip地址
        String ip = request.getRemoteAddr();
        log.setIp(ip);
        // 管理员信息
        Subject subject = SecurityUtils.getSubject();
        MarketAdmin admin = (MarketAdmin) subject.getPrincipals().getPrimaryPrincipal();
        //获得admin
        String username = admin.getUsername();
        log.setAdmin(username);
        //获得请求url
        String requestURI = request.getRequestURI();
        String uri = requestURI.replace("/admin/", "");
        //获得请求方法
        String method = request.getMethod();
        String api = method + " " + requestURI;
        if (uri.contains(Constant.ORDER_ACTION)) {
            //订单操作
            log.setType(Constant.ORDER_TYPE);
            String label = marketLogMapper.selectLabelByApi(api);
            log.setAction(Constant.ORDER_ACTION + label);
        } else if (uri.contains(Constant.ADMIN_ACTION)) {
            //安全操作
            log.setType(Constant.ADMIN_TYPE);
            String label = marketLogMapper.selectLabelByApi(api);
            log.setAction(Constant.ADMIN_ACTION + label);
        } else {
            //其他操作
            log.setType(Constant.OTHER_ACTION);
        }
        //执行方法和参数
        log.setAddTime(new Date());
        log.setUpdateTime(new Date());
        Object[] args = joinpoint.getArgs();
        Object result = joinpoint.proceed(args);
        //执行方法后
        BaseRespVo resultVo = (BaseRespVo) result;
        int errno = resultVo.getErrno();
        if (errno != 0) {
            //操作失败
            log.setStatus(false);
            String errmsg = resultVo.getErrmsg();
            log.setResult(errmsg);
        }
        //操作成功
        log.setStatus(true);
        marketLogMapper.insertSelective(log);
        return resultVo;
    }

}
